package com.now.printer.base;

import android.app.Application;
import android.content.Context;
import android.os.Build;
import android.os.StrictMode;
import android.util.Log;

import com.google.android.cameraview.CameraView;
import com.hjq.permissions.XXPermissions;
import com.luck.picture.lib.tools.PictureFileUtils;
import com.now.printer.BuildConfig;
import com.now.printer.R;
import com.now.printer.utils.sdkinit.AutoCameraStrategy;
import com.now.printer.utils.sdkinit.TbsInit;
import com.now.printer.utils.sdkinit.XBasicLibInit;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.DefaultRefreshFooterCreator;
import com.scwang.smartrefresh.layout.api.DefaultRefreshHeaderCreator;
import com.scwang.smartrefresh.layout.api.RefreshFooter;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.tencent.bugly.crashreport.CrashReport;
import com.umeng.analytics.MobclickAgent;
import com.umeng.commonsdk.UMConfigure;
import com.zh.pocket.PocketSdk;
import com.zh.pocket.common.config.ADConfig;


import org.xutils.x;

import androidx.multidex.MultiDex;
import androidx.multidex.MultiDexApplication;

/**
 * Created by xudong on 2021/5/13.
 */

public class MyApplication extends MultiDexApplication {

    private static Context mContext;

    //static 代码段可以防止内存泄露
    static {
        //设置全局的Header构建器
        SmartRefreshLayout.setDefaultRefreshHeaderCreator(new DefaultRefreshHeaderCreator() {
            @Override
            public RefreshHeader createRefreshHeader(Context context, RefreshLayout layout) {
                layout.setPrimaryColorsId(R.color.colorPrimary, android.R.color.white);//全局设置主题颜色
                return new ClassicsHeader(context);//.setTimeFormat(new DynamicTimeFormat("更新于 %s"));//指定为经典Header，默认是 贝塞尔雷达Header
            }
        });
        //设置全局的Footer构建器
        SmartRefreshLayout.setDefaultRefreshFooterCreator(new DefaultRefreshFooterCreator() {
            @Override
            public RefreshFooter createRefreshFooter(Context context, RefreshLayout layout) {
                //指定为经典Footer，默认是 BallPulseFooter
                return new ClassicsFooter(context).setDrawableSize(20);
            }
        });
    }

    @Override
    public void onCreate() {
        super.onCreate();
//        LibsLoadHelper.loadLibs(this);
        mContext = getApplicationContext();
        MultiDex.install(this);
        x.Ext.init(this);
        x.Ext.setDebug(BuildConfig.DEBUG);
        initUI();
        //三方SDK初始化
        TbsInit.init(this);
        //初始化基础库
        XBasicLibInit.init(this);
        // 当前项目是否已经适配了分区存储的特性
        XXPermissions.setScopedStorage(true);

        ADConfig.setEnableLogger(BuildConfig.DEBUG);
//      PocketSdk.initSDK(this, "", "10487");
        CrashReport.initCrashReport(getApplicationContext(), "7ea365bfac", BuildConfig.DEBUG);

        initVmPolicy();

        UMConfigure.setLogEnabled(BuildConfig.DEBUG);
        /**
         * 注意: 即使您已经在AndroidManifest.xml中配置过appkey和channel值，也需要在App代码中调
         * 用初始化接口（如需要使用AndroidManifest.xml中配置好的appkey和channel值，
         * UMConfigure.init调用中appkey和channel参数请置为null）。
         */
        UMConfigure.init(this,"60dfb2d426a57f101844af93","php upgrade",UMConfigure.DEVICE_TYPE_PHONE,"");
        //选择AUTO页面采集模式，统计SDK基础指标无需手动埋点可自动采集。
       //建议在宿主App的Application.onCreate函数中调用此函数。
        MobclickAgent.setPageCollectionMode(MobclickAgent.PageMode.AUTO);
    }

    /**
     * 使用VmPolicy方式检测FileUriExposure异常，解决7.0路径问题
     */
    private void initVmPolicy() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
            StrictMode.setVmPolicy(builder.build());
        }
    }

    /**
     * 初始化XUI 框架
     */
    private void initUI() {

        PictureFileUtils.setAppName("xui");

        CameraView.setICameraStrategy(new AutoCameraStrategy(1920 * 1080));
    }

    public static MyApplication getMyAppContext(){
        return (MyApplication) mContext;
    }

    public static boolean isDebug() {
        return BuildConfig.DEBUG;
    }

}
